#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // target
    ui->pushButton_target->setIcon(QIcon(":/img/kun.png"));
    ui->pushButton_target->setIconSize(QSize(80, 80));

    // 上
    ui->pushButton_up->setIcon(QIcon(":/img/button_up.png"));
    ui->pushButton_up->setIconSize(QSize(30, 30));
    // 下
    ui->pushButton_down->setIcon(QIcon(":/img/button_down.png"));
    ui->pushButton_down->setIconSize(QSize(30, 30));
    // 左
    ui->pushButton_left->setIcon(QIcon(":/img/button_left.png"));
    ui->pushButton_left->setIconSize(QSize(30, 30));
    // 右
    ui->pushButton_right->setIcon(QIcon(":/img/button_right.png"));
    ui->pushButton_right->setIconSize(QSize(30, 30));

    // 设置快捷键(必须在构造函数中,程序启动就生效)

    // 法1: 简单但是容易出错
    // ui->pushButton_up->setShortcut(QKeySequence("w")); /* QKeySequence("ctrl + w") */
    // ui->pushButton_down->setShortcut(QKeySequence("s"));
    // ui->pushButton_left->setShortcut(QKeySequence("a"));
    // ui->pushButton_right->setShortcut(QKeySequence("d"));

    // 法2: 枚举按键
    ui->pushButton_up->setShortcut(QKeySequence(Qt::Key_W)); /* QKeySequence(Qt::CTRL + Qt::Key_W) */
    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_S));
    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key_A));
    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key_D));

    // 快捷键默认带有 '连发' 效果,鼠标若需要连发需要设置autoRepeat属性
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_up_clicked()
{
    // 获取到up位置
    QRect rect = ui->pushButton_target->geometry();
    // 基于上次位置进行新的位置设置
    ui->pushButton_target->setGeometry(rect.x(), rect.y() - 5, rect.width(), rect.height());
}

void Widget::on_pushButton_down_clicked()
{
    // 获取到down位置
    QRect rect = ui->pushButton_target->geometry();
    // 基于上次位置进行新的位置设置
    ui->pushButton_target->setGeometry(rect.x(), rect.y() + 5, rect.width(), rect.height());
}

void Widget::on_pushButton_left_clicked()
{
    // 获取到left位置
    QRect rect = ui->pushButton_target->geometry();
    // 基于上次位置进行新的位置设置
    ui->pushButton_target->setGeometry(rect.x() - 5, rect.y(), rect.width(), rect.height());
}

void Widget::on_pushButton_right_clicked()
{
    // 获取到right位置
    QRect rect = ui->pushButton_target->geometry();
    // 基于上次位置进行新的位置设置
    ui->pushButton_target->setGeometry(rect.x() + 5, rect.y(), rect.width(), rect.height());
}
